<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资讯标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入资讯标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资讯ID" prop="articleId">
        <el-input
          v-model="queryParams.articleId"
          placeholder="请输入资讯ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序日期" prop="sortTimeStr">
        <el-date-picker
          clearable
          v-model="queryParams.sortTimeStr"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="请选择排序日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="资讯状态" prop="isPublish">
        <el-select
          v-model="queryParams.isPublish"
          filterable
          placeholder="请选择资讯状态"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="编辑人员" prop="updateBy">
        <el-select
          v-model="queryParams.auditBy"
          filterable
          clearable
          placeholder="请选择编辑人员"
        >
          <el-option
            v-for="item in userOptions"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间" prop="publishTime">
        <el-date-picker clearable
                        v-model="queryParams.publishTimeStr"
                        value-format="yyyy-MM-dd"
                        type="date"
                        placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="审核人员" prop="auditBy">
        <el-select
          v-model="queryParams.updateBy"
          filterable
          clearable
          placeholder="请选择审核人员"
        >
          <el-option
            v-for="item in userOptions"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="修改时间" prop="createTime">
        <el-date-picker
          clearable
          v-model="queryParams.updateTimeStr"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="请选择修改时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['article:article:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['article:article:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="articleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="no" />
      <el-table-column label="资讯标题" align="center" prop="title" />
      <el-table-column label="资讯ID" align="center" prop="articleId" />
      <el-table-column label="排序日期" align="center" prop="sortTime" />
      <el-table-column label="资讯状态" align="center" prop="status" />
      <el-table-column label="编辑人员" align="center" prop="auditName" />
      <el-table-column label="发布时间" align="center" prop="publishTimeStr" />
      <el-table-column label="审核人员" align="center" prop="updateName" />
      <el-table-column label="修改时间" align="center" prop="updateTime" />
      <el-table-column label="操作按钮" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['article:article:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['article:article:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章资讯信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1500px"
      :close-on-click-modal="false"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="form.title"
            type="textarea"
            placeholder="请输入标题"
          />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="文号" prop="proof">
              <el-input v-model="form.proof" placeholder="请输入文号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性质" prop="character">
              <el-select v-model="form.character" placeholder="请选择性质">
                <el-option
                  v-for="dict in dict.type.information_nature"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="简介" prop="instructions">
          <el-input
            v-model="form.instructions"
            type="textarea"
            placeholder="请输入简介"
          />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="成文日期" prop="completeTime">
              <el-date-picker
                clearable
                v-model="form.completeTime"
                type="date"
                placeholder="请选择成文日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序日期" prop="sortTime">
              <el-date-picker
                clearable
                v-model="form.sortTime"
                type="date"
                placeholder="请选择排序日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="对应批次" prop="year">
              <el-input v-model="form.year" placeholder="请输入对应批次" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否" prop="sourceType">
              <el-radio-group v-model="form.sourceType">
                <el-radio label="2">原创</el-radio>
                <el-radio label="1">转载</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="所属政策" prop="category">
          <el-cascader style="width: 800px;"
            v-model="form.category"
            placeholder="请选择所属政策"
            :options="policyOptions"
            :props="{ value: 'id', multiple, emitPath: false }"
            filterable
          ></el-cascader>
        </el-form-item>

          <el-form-item label="主管部门" prop="orgId">
            <el-cascader style="width: 800px;"
              v-model="form.orgIdList"
              placeholder="请选择主管部门"
              :options="orgOptions"
              :props="{emitPath:false, checkStrictly: true, multiple: true}"
              filterable></el-cascader>
          </el-form-item>

        <el-form-item label="来源网址" prop="source">
          <el-input
            v-model="form.source"
            type="textarea"
            placeholder="请输入来源网址"
          />
        </el-form-item>

        <el-form-item label="来源路径">
          <el-input
            v-model="form.navigation"
            type="textarea"
            placeholder="请输入来源路径"
          />
        </el-form-item>

        <el-form-item label="正文内容">
          <editor
            v-if="open"
            v-model="form.content"
            @contentData="contentData"
            :min-height="192"
          />
        </el-form-item>

        <el-form-item label="附件内容" prop="attachments">
          <editor
            v-if="open"
            v-model="form.attachments"
            @contentData="attachmentsData"
            :min-height="192"
          />
        </el-form-item>
        <el-form-item label="附件上传" prop="attachmentData">
                    <file-upload v-model="form.attachmentDataList"/>
<!--          <file-upload v-model="form.attachmentData" />-->
        </el-form-item>
        <el-form-item label="成果上传" prop="workResult">
                    <file-upload v-model="form.workResultList"/>
<!--          <file-upload v-model="form.workResult" />-->
        </el-form-item>
        <el-form-item label="是否发布" prop="isPublish">
          <el-radio-group v-model="form.isPublish">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <editor v-show="false" />
  </div>
</template>

<script>
import {
  listArticle,
  getArticle,
  delArticle,
  addArticle,
  updateArticle,
} from "@/api/article/article";
import { selectPolicyTree } from "@/api/article/article_policy";
import { getAllUser } from "@/api/system/user";
import {getAllOrg} from "@/api/org/org";

export default {
  name: "Article",
  dicts: ["information_nature"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章资讯信息表格数据
      articleList: [],
      // 政策下拉
      policyOptions: [],
      // 主管单位（树）
      orgOptions: [],
      // 状态下拉
      statusOptions: [
        { value: 0, label: "待审核" },
        { value: 1, label: "已发布" },
      ],
      // 人员下拉
      userOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        articleId: "",
        title: "",
        status: "",
        auditBy: "",
        updateBy: "",
        sortTimeStr: "",
        publishTimeStr: "",
        updateTimeStr: "",
        pageNum: 1,
        pageSize: 20,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getAllPolicy();
    this.getAllUser();
    this.getAllOrg();
  },
  methods: {
    /** 查询文章资讯信息列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then((response) => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAllOrg() {
      getAllOrg().then(response => {
        this.orgOptions = response.data;
      });
    },
    getAllPolicy() {
      selectPolicyTree().then((response) => {
        this.policyOptions = response.data;
      });
    },
    getAllUser() {
      getAllUser().then((response) => {
        this.userOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        articleId: null,
        proof: null,
        character: null,
        instructions: null,
        completeTime: null,
        publishTime: null,
        sortTime: null,
        source: null,
        year: null,
        isPublish: null,
        navigation: null,
        category: null,
        orgId: null,
        orgIdList: null,
        content: null,
        attachmentData: null,
        attachmentDataList: null,
        attachments: null,
        workResult: null,
        workResultList: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.articleId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章资讯信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const articleId = row.articleId || this.ids;
      getArticle(articleId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章资讯信息";
      });
    },
    contentData(e) {
      this.form.content = e;
    },
    attachmentsData(e) {
      this.form.attachments = e;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.articleId != null) {
            updateArticle(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const articleIds = row.articleId || this.ids;
      this.$modal
        .confirm('是否确认删除文章资讯信息编号为"' + articleIds + '"的数据项？')
        .then(function () {
          return delArticle(articleIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "article/article/export",
        {
          ...this.queryParams,
        },
        `article_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
